package mongoDB

import net.vz.mongodb.jackson.JacksonDBCollection
import net.vz.mongodb.jackson.MapReduce
import net.vz.mongodb.jackson.MapReduce.MapReduceCommand
import net.vz.mongodb.jackson.MapReduceOutput
import net.vz.mongodb.jackson.WriteResult
import com.mongodb.BasicDBObject

class Collection[T <: {def id:String}] {
	var mongoCollection: JacksonDBCollection[T, String] = null
  
	def this(collection: JacksonDBCollection[T, String]) {
	  	this
		mongoCollection = collection
	}
	
	def insert(objeto: T): WriteResult[T, String] = {
		return mongoCollection.insert(objeto)
    }
	
	def insertList(objeto: java.util.List[T]): WriteResult[T, String] = {
		return mongoCollection.insert(objeto);
    }
	
	def update(objeto: T) {
		mongoCollection.updateById(objeto.id, objeto)
	}
	
	def mapReduce[E, S](map: String, reduce: String, entrada: Class[E], salida: Class[S]): MapReduceOutput[E, S] = {
		return mongoCollection.mapReduce(mapReduceCommand(map, reduce, entrada, salida));
	}
	
	def mapReduce[E, S](map: String, reduce: String, finalize: String, entrada: Class[E], salida: Class[S]): MapReduceOutput[E,S] = {
		return mongoCollection.mapReduce(mapReduceCommand(map, reduce, finalize, entrada, salida));
	}
	
	def mapReduceCommand[E, S](map: String, reduce: String, entrada: Class[E], salida: Class[S]): MapReduceCommand[E,S] = {
		return MapReduce.build(map, reduce, MapReduce.OutputType.INLINE, null, entrada, salida);
	}
	
	def mapReduceCommand[E, S](map: String, reduce: String, finalize: String, entrada: Class[E], salida: Class[S]): MapReduceCommand[E,S] = {
		var command: MapReduceCommand[E, S] = this.mapReduceCommand(map, reduce, entrada, salida)
		command.setFinalize(finalize);
		return command;
	}
	
	def filterByVisibleDestination() {
		var query: BasicDBObject = new BasicDBObject
		query.put("visibility", "visible");
		mongoCollection.find(query)
	}
}